package net.lzzy.practices.utils;

import com.alibaba.fastjson.JSONObject;
import net.lzzy.practices.web.models.BaseInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * @author HUAWEI
 */
public class AspectUtil {
    private static final String ARG_TOKEN="token";

    public static String getToken(ProceedingJoinPoint point){
        Object[] args=point.getArgs();
        String[] names=((MethodSignature)point.getSignature()).getParameterNames();
        //1.直接列在方法参数中
        int i=0;
        for (String name:names){
            if (ARG_TOKEN.equals(name)){
                return args[i].toString();
            }
            i++;
        }
        if (args[0] instanceof BaseInfo){
            //2.使用了Info参数类BaseInfo
            return ((BaseInfo)args[0]).getToken();
        }else {
            //3.使用JSONObject对象
            JSONObject obj = (JSONObject) args[0];
            return obj.getString(ARG_TOKEN);
        }
    }
}
